<?php
    include_once 'db.php';
    session_start();
    if(!$_SESSION["passed"]) return false;

    define("VERNIER_NUM", 1); // 比实际请求的人数多x个,此处可调整
    $employee_need_list = $_POST['param'];
    if($employee_need_list) $employee_need_list = json_decode($employee_need_list, true);

    $resultArray = array();
    foreach($employee_need_list as $value) {
        $employee_type_code = $value['employeeTypeCode'];
        // $employee_type = $value['employeeType'];
        $employee_num = $value['employeeNum'];
        array_push($resultArray, getSuitableEmployee($employee_type_code, $employee_num));
    }

    echo json_encode($resultArray);

    function getSuitableEmployee($employee_type_code, $employee_num) {
        $query_str = "select u.user_code, u.user_name from l_user u, l_user_extend ue"
            ." where u.user_code = ue.user_code and ue.status = 0 and ue.label = %q"
            ." order by ue.score desc, u.last_modify desc limit 0, %i";
        $query_param = array($employee_type_code, ($employee_num + VERNIER_NUM));

        $result = dbOperate($query_str, $query_param);
        $employee_type = getEmployeeType($employee_type_code);
        $arr = array();
        while($row = mysql_fetch_assoc($result))
        {
          array_push($arr, $row);
        }
        $rows = array("employeeTypeCode" => $employee_type_code, "employeeNum" => $employee_num, "employees" => $arr,  "employeeType" => $employee_type);

        return ($rows);
    };

    function getEmployeeType($employee_type_code) {
        $query_str = "select o_desc from d_occupation where o_code = %q";
        $query_param = array($employee_type_code);
        $result = dbOperate($query_str, $query_param);
        $arr = array();
        while($row = mysql_fetch_assoc($result))
        {
          array_push($arr, $row);
        }
        return $arr[0]['o_desc'];
    }
?>